package com.iot08.common.config.mongodb;

import org.springframework.beans.factory.BeanFactory;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.convert.DefaultMongoTypeMapper;
import org.springframework.data.mongodb.core.convert.MappingMongoConverter;
import org.springframework.data.mongodb.core.mapping.MongoMappingContext;

/**
 * @version 1.0
 * @Description:
 * @time: 2020/8/22 10:05
 * @author: Rock
 */
@Configuration
@ConfigurationProperties(prefix = "spring.data.mongodb.admin")
public class AdminMongodbConfig extends MongoConfig {

    @Bean("adminMongoTemplate")
    @Override
    public MongoTemplate getMongoTemplate(MongoMappingContext context, BeanFactory beanFactory) {
        MongoTemplate mongoTemplate = new MongoTemplate(this.mongoDbFactory());
        MappingMongoConverter mongoMapping = (MappingMongoConverter) mongoTemplate.getConverter();
        mongoMapping.setCustomConversions(customConversions());
        mongoMapping.afterPropertiesSet();
        //Don't save _class to mongo
        mongoMapping.setTypeMapper(new DefaultMongoTypeMapper(null));
        return mongoTemplate;
    }
}
